﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections;
using System.Collections.Generic;
using Emulator.Base;
using Emulator.GameBoy;
using Emulator.GameBoy.Memory;
using Emulator.GameBoy.CPU;
using Emulator.GameBoy.CPU.Instructions;
using Emulator.GameBoyConfig;

namespace GameboyCPUALUTest
{
    [TestClass]
    public class GPUTest
    {
        GameBoyMemory mMemory = new GameBoyMemory();
        GameBoyCPU mCPU ;
        GameboyGPU mGPU;
        int[] TestDataList = new int[] { 0xFF,0x7F,0};

        public GPUTest()
        {
            mCPU = new GameBoyCPU(mMemory);
            mGPU = new GameboyGPU(mMemory);
            mCPU.ResetCPU();
        }

        [TestMethod]
        public void VRAMDataBoundTest()
        {
            mMemory.Write8(0x8000, 0x8);
            mMemory.Write8(0x8001, 0x9);
            mMemory.Write8(0x9FFF, 0x11);
            //mGPU.FlushFrameData(mMemory.MemoryData);
            Assert.AreEqual(0x8, mGPU.VRAM[0], "VRAM 0 index");
            Assert.AreEqual(0x9, mGPU.VRAM[1], "VRAM 1 index");
            Assert.AreEqual(0x11, mGPU.VRAM[8 * 1024-1], "VRAM 8*1024 index");
            Assert.AreEqual(8*1024, mGPU.VRAM.Length, "VRAM Lenght");
        }

        [TestMethod]
        public void Tile0DataBoundTest()
        {
            mMemory.Write8(0x8800, 0x8);
            mMemory.Write8(0x8801, 0x9);
            mMemory.Write8(0x97FF, 0x11);
            //mGPU.FlushFrameData(mMemory.MemoryData);
            Assert.AreEqual(0x8, mGPU.Tiles0Data[0], "Tiles0Data 0 index");
            Assert.AreEqual(0x9, mGPU.Tiles0Data[1], "Tiles0Data 1 index");
            Assert.AreEqual(0x11, mGPU.Tiles0Data[0xFFF], "Tiles0Data 0xFFF index");
            Assert.AreEqual(0x1000, mGPU.Tiles0Data.Length, "Tiles0Data length");
        }

        [TestMethod]
        public void Tile1DataBoundTest()
        {
            mMemory.Write8(0x8000, 0x8);
            mMemory.Write8(0x8001, 0x9);
            mMemory.Write8(0x8FFF, 0x11);
            //mGPU.FlushFrameData(mMemory.MemoryData);
            Assert.AreEqual(0x8, mGPU.Tiles1Data[0], "Tiles1Data 0 index");
            Assert.AreEqual(0x9, mGPU.Tiles1Data[1], "Tiles1Data 1 index");
            Assert.AreEqual(0x11, mGPU.Tiles1Data[0xFFF], "Tiles1Data 0xFFF index");
            Assert.AreEqual(0x1000, mGPU.Tiles1Data.Length, "Tiles1Data length");
        }

        [TestMethod]
        public void Tile0MapDataBoundTest()
        {
            mMemory.Write8(0x9800, 0x8);
            mMemory.Write8(0x9801, 0x9);
            mMemory.Write8(0x9BFF, 0x11);
            //mGPU.FlushFrameData(mMemory.MemoryData);
            Assert.AreEqual(0x8, mGPU.Tiles0Map[0], "Tile0Map 0 index");
            Assert.AreEqual(0x9, mGPU.Tiles0Map[1], "Tile0Map 1 index");
            Assert.AreEqual(0x11, mGPU.Tiles0Map[0x3FF], "Tile0Map 0x3FF index");
            Assert.AreEqual(0x400, mGPU.Tiles0Map.Length, "Tile0Map length");
        }

        [TestMethod]
        public void Tile1MapDataBoundTest()
        {
            mMemory.Write8(0x9C00, 0x8);
            mMemory.Write8(0x9C01, 0x9);
            mMemory.Write8(0x9FFF, 0x11);
            //mGPU.FlushFrameData(mMemory.MemoryData);
            Assert.AreEqual(0x8, mGPU.Tiles1Map[0], "Tile1Map 0 index");
            Assert.AreEqual(0x9, mGPU.Tiles1Map[1], "Tile1Map 1 index");
            Assert.AreEqual(0x11, mGPU.Tiles1Map[0x3FF], "Tile1Map 0x3FF index");
            Assert.AreEqual(0x400, mGPU.Tiles1Map.Length,"Tile1Map length");
        }
    }
}
